import React, { Fragment, PropsWithChildren } from "react";
import { systemPermission } from "@/components/app/sys";

import LoginUser from './LoginUser'

class Permission {
  loginUser = new LoginUser();

  // 验证是否为超级管理员
  // isSuperAdmin = () => this.loginUser.get().isSuperAdmin;
  isSuperAdmin = () => false;

  can = (auth: string | { key: string }) => {
    if (this.isSuperAdmin()) return true;

    const { permissions } = this.loginUser.get();

    if (!auth || !permissions) {
      return false
    }

    if (typeof auth === 'string') {
      return permissions.includes(auth);
    } else {
      return permissions.includes(auth.key);
    }
  }
}

export const permission = new Permission()

/**
 * 权限组件
 */
export const AuthCan: React.FC<PropsWithChildren<{ authKey: string }>> = ({ authKey, children }) => React.createElement(Fragment, { children: permission.can(authKey) && children })

/**
 * 权限key定义
 * 格式规范是: 
 *  "EnumRouter中的key":{
 *    key: "系统中页面中配置的路由path"
 * }
 */
export const EnumAuthKey = {
  //-------------------------系统设置相关的路由------------------------------
  ...systemPermission,
}
